<?php
/**
 * Created by PhpStorm.
 * User: moonlight
 * Date: 2015/5/24
 * Time: 23:29
 */

namespace Home\Model;


use Think\Model;

class UserModel extends Model{
    protected $patchValidate = true;
    protected $_map = array(
        'regUserName' =>'user_name', // 把表单中name映射到数据表的username字段
        'regPassword'  =>'user_password', // 把表单中的mail映射到数据表的email字段
        );
    protected  $_validate = array(
        array('user_name','/^[\w\d\-]{4,20}$/','用户名不满足要求！英文，数字，下划线且4-20位。'),
        array('user_password','require','密码不能为空'),
        array('regPassword2','require','确认密码不能为空'),
        array('regPassword2','user_password','确认密码不正确',0,'confirm'), // 验证确认密码是否和密码一致
        array('user_name','','用户名已存在',0,'unique',1 ),
        );
    protected $_auto = array(
        array('user_password','sha1',3,'function'),
        array('last_time','time',3,'function'),
        array('creat_ip','get_client_ip',1,'function'),
        array('creat_time','time',1,'function'),
        array('last_ip','get_client_ip',3,'function'),
        );
    
    public function register(){
        if(!$this->create()){
            return $this->getError();
        }else{

            $msg = '注册成功';
            if($this->add()){
              $data['reg_msg'] = $msg;
              $data['status']  = 1;
              
              return $data;
          }
          $msg = '注册失败！';
          $data['reg_msg'] = $msg;
          $data['status']  = 0;
          return $data;
      }
  }
    //登录
  public function login($data){
    $user_name = $data['logUserName'];
    $user_password = $data['logPassword'];
    $info = array(
       'status' => 1,
       'info'   => '登陆成功！',
       'errorarea'  => '.msg-log',
       );
       //用户名不能为空
    if(!$user_name){
       $info = array(
         'status' => 0,
         'info'   => '用户名不能为空',
         'errorarea'  => '.msg-name',
         );
       return $info;
   }
       //密码不能为空
   if(!$user_password){
       $info = array(
         'status' => 0,
         'info'   => '密码不能为空',
         'errorarea'  => '.msg-pwd',
         );
       return $info;
   }
       //用户名是否存在
   $condition['user_name'] = $user_name;
   $result = $this->where($condition)->find();
   if(!$result){
       $info = array(
         'status' => 0,
         'info'   => '用户名不存在！',
         'errorarea'  => '.msg-name',
         );
       return $info;
   }
       //密码是否正确
   $user_password = sha1($user_password);
   if($user_password != $result['user_password']){
       $info = array(
         'status' => 0,
         'info'   => '密码不正确！',
         'errorarea'  => '.msg-pwd',
         );
       return $info;
   }
   $data['last_time'] = time();
   $data['last_ip'] = $_SERVER['REMOTE_ADDR'];
   $this->where($result)->save($data);
   session('username',$user_name);
   session('user_id',$result['user_id']);
   //dump($info);
   //$info['url'] = U('Home/Index/index');
   return $info;
}


    //判断用户信息是否完善，能否进行发布消息
    public function isComplete(){
      $condition['user_id'] = session('user_id');
      $result = $this->where($condition)->find();
      if(!empty($result['studentnumber']) && !empty($result['telephone'])){
          return ture;
      }else{

          return false;
      }
      
    }

    /**
     * Id To Name
     */
    public function idToName($data){
        $count = count($data);
        $i = 0;
        $list   =   $data;
        while($i < $count){
            $condition['user_id']   =   $data[$i]['user_id'];
            $result     =   $this->where($condition)->find();
            $list[$i]['user_name']   =  $result['user_name'];
            $i++;
        }
        return  $list;

    }

    /**
     * 通过id获得用户信息
     *
     * @param $user_id
     * @return array
     */
    public function getUserData($user_id){
        if(empty($user_id)){//nidongde
            return '用户id为空';    //想到个问题，如果查询结果也为空呢？貌似没有对这个做处理
        }else{
            $condition['user_id']   =   $user_id;
            $result =   $this->where($condition)->find();
            if($result){
                return $result;
            }
            return false;
        }
        return;
    }

    public function updateUserInfo($data){
        $data = array_filter($data);
        $condition['user_id']    =   session('user_id');
        $result = $this->where($condition)->save($data);
        if($result){
            return ture;
        }
        return false;
    }
}

